<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css">
    <link href="framework/reset.css" rel="stylesheet">
    <link href="css/order.css" rel="stylesheet">
    <script src="js/pageJump.js"></script>
    <script src="https://fastly.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <title>饿了么 确认订单</title>
</head>

<body>
    <div class="wrapper" id="app" v-if="cart.business!=null">

        <!-- header部分 -->
        <header>
            <p>确认订单</p>
        </header>

        <!-- 订单信息部分 -->
        <div class="order-info" @click="jumpTo('userAddress.html?id='+cart.business.businessId)" v-if="address!=null">
            <h5>订单配送至：</h5>
            <div class="order-info-address">
                <p>{{getActiveAddress.address}}</p>
                <i class="fa fa-angle-right"></i>
            </div>
            <p>{{getActiveAddress.name+(getActiveAddress.sex==0?"先生":"女士")+" "+getActiveAddress.phone}}</p>
        </div>

        <h3>{{cart.business.businessName}}</h3>
        <!-- 订单明细部分 -->
        <ul class="order-detailed">
            <li v-for="food in cart.foods">
                <div class="order-detailed-left">
                    <img :src="food.foodImg">
                    <p>{{food.foodName}} x {{food.quantity}}</p>
                </div>
                <p>&#165;{{food.foodPrice}}</p>
            </li>
        </ul>
        <div class="order-deliveryfee">
            <p>配送费</p>
            <p>&#165;{{cart.business.deliveryPrice}}</p>
        </div>

        <!-- 合计部分 -->
        <div class="total">
            <div class="total-left">&#165;{{getTotalPrice}}</div>
            <div class="total-right" @click="createOrder">
                去支付
            </div>
        </div>
    </div>
    <script>
        var param=getRequest()
        var path=getRequestPath()
        var app=new Vue({
            el:"#app",
            data:{
                
                cart:{business:{}},
                address:null
            },
            methods:{
                getCart:function(){
                    axios.post(path+"/CartController/listCart",null,{
                        params:{
                            businessId:param.id,
                        },
                        headers:{
                            Authorization:this.getAuthorization()
                        }
                    }).then((respon)=>{
                        if(respon.data.code==200){
                            this.cart=respon.data.detail
                        }
                    }).catch((error)=>{
                        console.log(error)
                    })
                },
                getAddress:function(){
                    axios.post(path+"/DeliveryAddressController/listDeliveryAddressByUserId",null,{
                        params:{
                            
                        },
                        headers:{
                            Authorization:this.getAuthorization()
                        }
                    }).then((respon)=>{
                        if(respon.data.code==200){
                            this.address=respon.data.detail
                        }
                    }).catch((error)=>{
                        console.log(error)
                    })
                },
                createOrder:function(){
                    axios.post(path+"/OrdersController/createOrders",null,{
                        params:{
                            businessId:param.id,
                            daId:this.getActiveAddress.addressId,
                            orderTotal:this.getTotalPrice
                        },
                        headers:{
                            Authorization:this.getAuthorization()
                        }
                    }).then((respon)=>{
                        if(respon.data.code==200){
                            jumpTo("/payment.html?orderId="+respon.data.detail)
                        }
                    }).catch((error)=>{
                        console.log(error)
                    })
                },
                jumpTo:function(u){
                    
                    window.location.href=u
                },
                getAuthorization:function(){
                    return window.localStorage.getItem("Auth")
                }
            },
            created(){
                console.log(param.id)
                this.getCart()
                this.getAddress()
                
            },
            computed:{
                getActiveAddress:function(){
                    if(this.address!=null)
                    return this.address[param.daId!=null?param.daId:0]
                },
                getTotalPrice:function(){
                    var sum=0;
                    if(this.cart.foods!=null)
                    this.cart.foods.forEach(element => {
                        sum+=element.quantity*element.foodPrice
                        
                    })
                    return sum+this.cart.business.deliveryPrice
                }
            }
        })
    </script>
</body>

</html>